package io.shallwe._admin.basedata.init;

import com.google.common.collect.Maps;
import com.jfinal.aop.Aop;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.ehcache.CacheKit;
import io.shallwe._admin.basedata.hangerframe.HangerFrameService;
import io.shallwe.common.model.HangerFrameMiddle;
import io.shallwe.common.model.StaticHanger;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StaticMapService {

    private StaticHanger staticHangerDao = new StaticHanger().dao();

    @Inject
    private HangerFrameService hangerFrameService;

    public  int countHangerInLine(){
        Integer count =CacheKit.get("countOfHangerInLine","countOfHangerInLine");
        if(count==null){
            count = Db.queryInt("select count(*) from t_static_hanger where enable=1");
            CacheKit.put("countOfHangerInLine","countOfHangerInLine",count);
        }
        return count;
    }

    /***
     * 根据当前索引号计算出顺序数组
     * @param indexNo
     * @return
     */
    public int[] listHangerSortByScannerCode(int indexNo){
        int index=indexNo;
        int countOfHangerInLine = this.countHangerInLine();
        if(index>countOfHangerInLine){
            index=countOfHangerInLine;
        }
        if(index<1){
            index=1;
        }
        int[] array = new int[countOfHangerInLine];
        for (int i = 0; i < array.length; i++) {
            array[i]=index;
            index--;
            if(index==0){
                index=countOfHangerInLine;
            }
        }
        return array;
    }



    /**
     * 根据当前索引获得下一个索引
     * @param currentIndexNo
     * @return
     */
    public int getNextIndexNo(int currentIndexNo){
        int countOfHangerInLine = this.countHangerInLine();
        if(currentIndexNo>=countOfHangerInLine){
            return countOfHangerInLine;
        }
        return currentIndexNo+1;
    }

    /***
     * 根据当前索引获得上一个索引
     * @param currentIndexNo
     * @return
     */
    public int getLastIndexNo(int currentIndexNo){
        int countOfHangerInLine = this.countHangerInLine();
        if(currentIndexNo<2){
            return countOfHangerInLine;
        }
        return currentIndexNo-1;
    }





    /**
     * 列出所有挂具、电泳框号绑定信息
     * @return
     */
    public List<StaticHanger> listStaticHangers(){
        List<StaticHanger> staticHangers = CacheKit.get("listStaticHangers","listStaticHangers");
        if(staticHangers==null){
            staticHangers = staticHangerDao.template("initInformation.listStaticHangers").find();
            CacheKit.put("listStaticHangers" , "listStaticHangers" , staticHangers);
        }
        return staticHangers;
    }
    /**
     * 顺序与工位对照表
     * @return
     */
    public Map<Integer,StaticHanger> getHangerSortMap (){
        List<StaticHanger> staticHangers = this.listStaticHangers();
        return staticHangers.stream().collect(Collectors.toMap(staticHanger->staticHanger.getSort(),staticHanger -> staticHanger));
    }



    public Map<String,String> createOffLineScannerCodeAndOnLineScannerCodeMap(){
        Map<String,String> ret = CacheKit.get("createOffLineScannerCodeAndOnLineScannerCodeMap","OFF_ON");
        if(ret==null){
            ret = Maps.newHashMap();
            Map<Integer, StaticHanger> hangerSortMap = this.getHangerSortMap();
            for(int indexNo=1;indexNo<210;indexNo++) {
                HangerFrameMiddle onlineScanner = hangerFrameService.findById(indexNo);
                int[] array = this.listHangerSortByScannerCode(indexNo);
                int length = array.length;
                for (int i = 0; i < length; i++) {
                    int sort = i + 1;
                    int id = array[i];
                    HangerFrameMiddle offLineScanner = hangerFrameService.findById(id);
                    StaticHanger staticHanger = hangerSortMap.get(sort);
                    String locationCode = staticHanger.getLocationCode();
                    if (StrKit.equals("SCANNER_OUT", locationCode)) {
                        ret.put(offLineScanner.getHangerNo(),onlineScanner.getHangerNo());
                        break;
                    }
                }
            }
            CacheKit.put("createOffLineScannerCodeAndOnLineScannerCodeMap","OFF_ON",ret);
        }
        return ret;
    }


    public String getOnLineScannerCodeByOffLineHangerNo(String offLineCode){
        Map<String, String> offLineScannerCodeAndOnLineScannerCodeMap = this.createOffLineScannerCodeAndOnLineScannerCodeMap();
        return offLineScannerCodeAndOnLineScannerCodeMap.get(offLineCode);
    }


    public Map<String,String> createOnLineScannerCodeAndOffLineScannerCodeMap(){
        Map<String,String> ret = CacheKit.get("createOffLineScannerCodeAndOnLineScannerCodeMap","ON_FF");
        if(ret==null){
            ret = Maps.newHashMap();
            Map<Integer, StaticHanger> hangerSortMap = this.getHangerSortMap();
            for(int indexNo=1;indexNo<210;indexNo++) {
                HangerFrameMiddle onlineScanner = hangerFrameService.findById(indexNo);
                int[] array = this.listHangerSortByScannerCode(indexNo);
                int length = array.length;
                for (int i = 0; i < length; i++) {
                    int sort = i + 1;
                    int id = array[i];
                    HangerFrameMiddle offLineScanner = hangerFrameService.findById(id);
                    StaticHanger staticHanger = hangerSortMap.get(sort);
                    String locationCode = staticHanger.getLocationCode();
                    if (StrKit.equals("SCANNER_OUT", locationCode)) {
                        ret.put(onlineScanner.getHangerNo(),offLineScanner.getHangerNo());
                        break;
                    }
                }
            }
            CacheKit.put("createOffLineScannerCodeAndOnLineScannerCodeMap","ON_FF",ret);
        }
        return ret;
    }

    public String getOffLineScannerCodeByOnLineHangerNo(String offLineCode){
        Map<String, String> onLineScannerCodeAndOffLineScannerCodeMap = this.createOnLineScannerCodeAndOffLineScannerCodeMap();
        return onLineScannerCodeAndOffLineScannerCodeMap.get(offLineCode);
    }

}